package com.yantu.controller.admin;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yantu.bean.*;
import com.yantu.mapper.MessageMapper;
import com.yantu.mapper.MessageUserMapper;
import com.yantu.mapper.UserEventLogMapper;
import com.yantu.mapper.UserMapper;
import com.yantu.util.RedisUtils;
import com.yantu.util.Result;
import com.yantu.view.admin.AdminPageView;
import com.yantu.view.admin.MessageView;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.Date;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 综合实训课程设计
 * @since 2022-08-15
 */
@RestController
@RequestMapping("/admin")
@Api("管理端消息接口")
public class AdminMessageController extends AdminUserEventLogController{

    @Autowired
    RedisUtils redisUtils;

    @Autowired
    MessageMapper messageMapper;

    @Autowired
    UserMapper userMapper;

    @Autowired
    MessageUserMapper messageUserMapper;

    //消息列表
    @GetMapping("/messageList")
    @ApiOperation("消息列表")
    public Result myListsMessage(){
        if(redisUtils.haskeysf("adminMessageList")){
            System.out.println("缓存里面有");
            List<Message> messageList = (List<Message>) redisUtils.getValue("adminMessageList");
            return Result.succ(200, "查询成功", messageList);
        }else {
            List<Message> messageList = messageMapper.selectList(null);
            return Result.succ(200, "查询成功", messageList);
        }
    }

    //消息分页
    @GetMapping("messages")
    @ApiOperation("消息分页")
    public Result messageList(@ModelAttribute AdminPageView pageView){
        PageHelper.startPage(pageView.getPageIndex(), pageView.getPageSize());
        if(redisUtils.haskeysf("adminMessageList"+pageView)){
            System.out.println("缓存里面有");
            PageInfo pageInfo = (PageInfo) redisUtils.getValue("adminMessageList"+pageView);
            return Result.succ(200, "查询成功", pageInfo);
        }else {
            QueryWrapper<Message> queryWrapper = new QueryWrapper<>();
            if (pageView.getSendUserName() != null) {
                queryWrapper.eq("send_user_name", pageView.getSendUserName());
            }
            List<Message> messageList = messageMapper.selectList(queryWrapper);
            PageInfo pageInfo = new PageInfo(messageList);
            redisUtils.saveValue("adminMessageList"+pageView,pageInfo);
            return Result.succ(200, "查询成功", pageInfo);
        }
    }

    //消息发送
    @PostMapping("/message")
    @ApiOperation("消息发送")
    public Result messageSend(@RequestBody MessageView messageView){
        Message message=new Message();
        message.setTitle(messageView.getTitle());
        message.setContent(messageView.getContent());
        message.setSendUserId(2);
        message.setSendRealName("admin");
        //插入数据库message
        int result=messageMapper.insert(message);
        if(result>0){
            System.out.println("消息插入成功--message表");
            MessageUser messageUser=new MessageUser();

            User user=userMapper.selectById(messageView.getReceiveUserId());
            System.out.println("根据接收者id查询到接收者"+user);
            //初始化messageUser对象
            messageUser.setMessageId(message.getId());
            messageUser.setReceiveUserId(user.getId());
            messageUser.setReceiveUserName(user.getUserName());
            messageUser.setReceiveRealName(user.getRealName());
            messageUser.setReaded(0);
            messageUser.setCreateTime(new Date());
            //插入数据库message_user
            int result1=messageUserMapper.insert(messageUser);
            if(result1>0) {
                //管理员登录的日志
                super.adminLog("admin给用户"+user.getId()+"发送了消息"+message.getId());
                redisUtils.delAll();
                return Result.succ("发送成功",null);
            }else{
                messageMapper.deleteById(message.getId());
                return Result.fail("添加失败",null);
            }
        }else{
            return Result.fail("添加失败",null);
        }
    }

    //消息模糊查询
    @GetMapping("message/{name}")
    @ApiOperation("消息模糊查询（发送者用户名）")
    public Result selectMessage(@PathVariable("name") String name){
        QueryWrapper<Message> queryWrapper = new QueryWrapper<>();
        queryWrapper.like("send_user_name",name);
        List<Message> messageList=messageMapper.selectList(queryWrapper);
        return Result.succ(200,"查询成功",messageList);
    }
}

